package com.qnzw.zjmy.home.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.android.volley.Request;
import com.qnzw.zjmy.R;
import com.qnzw.zjmy.config.UrlConfig;
import com.qnzw.zjmy.home.ui.adapter.InviteRecordAdapter;
import com.qnzw.zjmy.home.ui.enums.UserEffectiveStatusEnum;
import com.qnzw.zjmy.home.ui.model.CountFriend;
import com.qnzw.zjmy.home.ui.model.InviteRecord;
import com.qnzw.zjmy.refresh.LoadingFooter;
import com.qnzw.zjmy.refresh.xRecyclerView;
import com.qnzw.zjmy.volley.BaseActivity;
import com.qnzw.zjmy.volley.state.HUCState;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.util.StrUtil;

/**
 * 邀请记录
 */
public class InviteRecordActivity extends BaseActivity {

    private xRecyclerView xRecyclerView;
    private InviteRecordAdapter inviteRecordAdapter;
    private List<InviteRecord> inviteRecordList = new ArrayList<>();
    private TextView tvCount, tvNickName, tvCode;
    private TextView tvYes, tvNo;
    private boolean refreshFlag = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_invite_record);
        super.onCreate(savedInstanceState);
        pageVo.setToken(getSharedPreferencesToken("token"));
        pageVo.setStatus(UserEffectiveStatusEnum.USER_EFFECTIVE_STATUS_NORMAL.getValue());
        countData();
        tvActionbarTitle.setText("推荐列表");
        inviteRecordAdapter = new InviteRecordAdapter(this, inviteRecordList, R.layout.item_invite_record);
        xRecyclerView.setAdapter(this, inviteRecordAdapter, new LinearLayoutManager(this));
        xRecyclerView.setOnRefreshListener(this);
        tvCode.setText("ID：" + getSharedPreferences("id"));
        tvNickName.setText(getSharedPreferences("nickname"));

    }

    @Override
    public void initView() {
        super.initView();
        xRecyclerView = findViewById(R.id.xrecyclerView);
        tvCount = findViewById(R.id.tv_count);
        tvNickName = findViewById(R.id.tv_nickname);
        tvCode = findViewById(R.id.tv_code);

        tvYes = findViewById(R.id.tv_yes);
        tvNo = findViewById(R.id.tv_no);
    }

    @Override
    public void initClick() {
        super.initClick();
        tvYes.setOnClickListener(this);
        tvNo.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_yes:
                tvYes.setBackground(getResources().getDrawable(R.color.home));
                tvYes.setTextColor(getResources().getColor(R.color.white));
                tvNo.setBackground(getResources().getDrawable(R.color.white_hui));
                tvNo.setTextColor(getResources().getColor(R.color.default_font_size));
                pageVo.setStatus(UserEffectiveStatusEnum.USER_EFFECTIVE_STATUS_NORMAL.getValue());
                onStart();
                break;
            case R.id.tv_no:
                tvNo.setBackground(getResources().getDrawable(R.color.home));
                tvNo.setTextColor(getResources().getColor(R.color.white));
                tvYes.setBackground(getResources().getDrawable(R.color.white_hui));
                tvYes.setTextColor(getResources().getColor(R.color.default_font_size));
                pageVo.setStatus(UserEffectiveStatusEnum.USER_EFFECTIVE_STATUS_VIOLATION.getValue());
                onStart();
                break;
        }
    }

    /**
     * 下拉刷新
     *
     * @param refreshLayout
     */
    @Override
    public void refresh(SwipeRefreshLayout refreshLayout) {
        refreshFlag = false;
        onStart();
    }

    /**
     * 上拉加载
     *
     * @param recyclerView
     */
    @Override
    public void loadMore(RecyclerView recyclerView) {
        refreshFlag = true;
        int CurrentCount = Integer.parseInt(pageVo.getCurrent()) + 1;
        pageVo.setCurrent(CurrentCount + "");
        pageVo.setSize(20 + "");
        getInitData();
    }

    private void getInitData() {
        hucRequestInterfase.executeStr(this, InviteRecord.class,
                HUCState.HUC_REQEUST_CODE_1, Request.Method.POST, UrlConfig.URL_USER_USER_FRIEND,
                hucParameInterfase.getRequestParameter(pageVo), false);
        loading();
    }

    private void countData() {
        hucRequestInterfase.executeStr(this, CountFriend.class,
                HUCState.HUC_REQEUST_CODE_2, Request.Method.POST, UrlConfig.URL_USER_COUNT_FRIEND,
                hucParameInterfase.getRequestParameter(pageVo), true);
    }

    @Override
    public void onSuccessfulStrType(int inputType, Object obj) {
        switch (inputType) {
            case HUCState.HUC_REQEUST_CODE_1:
                if (obj instanceof List) {
                    List<InviteRecord> inviteRecords = (List<InviteRecord>) obj;
                    if (refreshFlag) {
                        if (inviteRecords == null || inviteRecords.size() == 0) {
                            if (inviteRecords.size() == 0) {
                                xRecyclerView.refreshComplete(LoadingFooter.State.NoData, "");
                            } else {
                                xRecyclerView.refreshComplete(LoadingFooter.State.TheEnd, "");
                            }
                        } else {
                            for (InviteRecord inviteRecord : inviteRecords) {
                                inviteRecordList.add(inviteRecord);
                            }
                            inviteRecordAdapter.notifyDataSetChanged();
                            xRecyclerView.refreshComplete(LoadingFooter.State.Normal, "");
                        }
                    } else {
                        inviteRecordList.clear();
                        for (InviteRecord inviteRecord : inviteRecords) {
                            inviteRecordList.add(inviteRecord);
                        }
                        inviteRecordAdapter.notifyDataSetChanged();
                        xRecyclerView.refreshComplete(LoadingFooter.State.Normal, "");
                    }
                }
                break;
            case HUCState.HUC_REQEUST_CODE_2:
                if (obj instanceof CountFriend) {
                    CountFriend countFriend = (CountFriend) obj;
                    if (StrUtil.isEmpty(countFriend.getCount())) {
                        return;
                    }
                    tvCount.setText(countFriend.getCount());
                    tvYes.setText("有效（" + countFriend.getYesCount() + "）");
                    tvNo.setText("无效（" + countFriend.getNoCount() + "）");
                }
                break;
            default:
                break;
        }
        loadDismiss();
    }

    @Override
    protected void onStart() {
        pageVo.setToken(getSharedPreferencesToken("token"));
        super.onStart();
        refreshFlag = false;
        pageVo.setCurrent(1 + "");
        pageVo.setSize(20 + "");
        getInitData();
    }

}